package com.echi.watermarker;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@RequestMapping("file")
@RestController
public class FileController {
    @GetMapping("downloadFile/{fileName}")
    public void downloadFile(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        //需要添加水印的文件
        String inputFile = "C:\\Users\\Administrator\\Desktop\\IT联盟报名系统说明.pdf";
        //添加完水印的文件存放路径
        String outputFile = "C:\\Users\\Administrator\\Desktop\\testMulti.pdf";
        //需要添加的水印文字
        String waterMarkName = "测试多个水印";
        //水印字体透明度
        float opacity = 0.3f;
        //水印字体大小
        int fontsize = 20;
        //水印倾斜角度（0-360）
        int angle = 30;
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            reader = new PdfReader(inputFile);
            stamper = new PdfStamper(reader, Files.newOutputStream(Paths.get(outputFile)));
            PdfWaterMarkerUtil.addWaterMarkOnPdfReader(reader, stamper, waterMarkName, opacity, fontsize, angle, 100, true);
        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (stamper != null) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                reader.close();
            }
        }
//        PdfWaterMarkerUtil.addWaterMark(inputFile, outputFile, waterMarkName, opacity, fontsize, angle, 150);
    }
}
